package com.xiaojie.edi.mybatisplus;

import com.google.common.collect.ImmutableMap;
import com.xiaojie.dao.EDIDataOperate;
import com.xiaojie.dao.Param;
import com.xiaojie.edi.mybatisplus.dao.EDIQueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 数据操作
 *
 * @author wangye
 * @classname DataOperationImpl
 * @date 2020/12/21 10:16
 **/
@Component
public class EDIDataOperationImpl implements EDIDataOperate {

    @Autowired
    private EDIQueryMapper queryMapper;

    @Autowired
    private FieldTypeHandleService fieldTypeHandleService;

    @Override
    public List<Map> selectData(String tableName, String fields, String paramName, Object paramValue, String split) {
        Map paramMap = ImmutableMap.of(paramName, paramValue);
        return queryMapper.selectByMaps(tableName, fields, paramMap, split);
    }


    @Override
    public List<Map> selectData(String tableName, String fields, List<Param> params, String split) {
        if (params == null) {
            return selectAll(tableName, fields);
        }
        Map<String, Object> paramMap = params.stream().collect(Collectors.toMap(Param::getName, Param::getValue));
        List<Map> maps = queryMapper.selectByMaps(tableName, fields, paramMap, split);
        fieldTypeHandleService.exportHandleDataByFieldType(tableName, maps);
        return maps;
    }


    @Override
    public List<Map> selectAll(String tableName, String fields) {
        return queryMapper.selectAll(tableName, fields);
    }

    @Override
    public void insert(String sql) {
        queryMapper.batchInsert(sql);
    }

}
